import 'package:fish_redux/fish_redux.dart';
import 'package:hayll_app_village_master/hayll/village_manager/home/main_page/bean/company.dart';

import 'action.dart';
import 'state.dart';

Reducer<MainState> buildReducer() {
  return asReducer(
    <Object, Reducer<MainState>>{
      MainAction.refresh:_refreshUI,
      // MainAction.updateCompanyList: _updateCompanyList,
      // MainAction.switchCompany: _switchCompany,
    },
  );
}

MainState _updateCompanyList(MainState state, Action action) {
  final MainState newState = state.clone()..commanyList = action.payload;
  if (newState.commanyList != null && newState.commanyList.isNotEmpty) {
    newState.selectedCompany = newState.commanyList.first;
  }

  return newState;
}

MainState _switchCompany(MainState state, Action action) {
  final MainState newState = state.clone();
  if (newState.commanyList != null) {
    for (CompanyResult item in newState.commanyList) {
      if (item.companyId == action.payload) {
        newState.selectedCompany = item;
        break;
      }
    }
  }

  return newState;
}

MainState _refreshUI(MainState state, Action action) {
  final MainState newState = state.clone();
  // if (newState.commanyList != null) {
  //   for (CompanyResult item in newState.commanyList) {
  //     if (item.companyId == action.payload) {
  //       newState.selectedCompany = item;
  //       break;
  //     }
  //   }
  // }

  return newState;
}